Skip to content

Refuse an argument with a NUL character before starting the program - #119

Merged
HalRiveria merged 11 commits into
mainfrom
fix/refuse-nul-argument
Dec 3, 2026
Merged

HalRiveria merged 11 commits into
mainfrom
fix/refuse-nul-argument

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

Depends on #117, merge after it.

What was wrong: Windows reads a command line up to its first NUL character. An argument holding one was cut off there, and every argument after it was dropped, with no error. cmd /c echo before<NUL>after next printed before. This happened on both start paths, with list and string -Arguments alike.

What changed: a NUL in an argument is now an error before the program starts:

  • In a list, ConvertTo-PkArgumentStringCore refuses it and names the element, as it does for a line break bound for cmd.exe. That covers ConvertTo-PkArgumentString too.
  • In a string -Arguments, Start-PkProcessCore refuses it.

Start-PkProcess and Invoke-PkProcessWithTimeout throw. In a batch, it's that command's StartError. The README's Arguments section says so.

How it was tested: harness section AE passes a NUL through Invoke-PkProcessWithTimeout (in a list and in a string), Start-PkProcess, Invoke-PkProcessBatch (both forms) and ConvertTo-PkArgumentString.

Before the fix (Windows PowerShell 5.1 and PowerShell 7, the same on both):

[FAIL] a NUL in an argument is refused before the program starts, in a list or a string (list: ; string: ; Start-PkProcess: )
[FAIL] and is the command's StartError in a batch ( | )
[FAIL] and ConvertTo-PkArgumentString refuses it too ()

After: all three pass, for example Arguments element 2 contains a NUL character, which can't be passed on a command line: Windows would end the command line there and drop the rest, later arguments included. Pass it another way (a file, an environment variable).

Full harness: all 774 assertions pass on Windows PowerShell 5.1 and on PowerShell 7.

Batch-file quoting was picked by a pattern that needed a slash or the start of the path before 'cmd', so C:cmd.exe and C:cmd got the ordinary quoting, where a quote is written \" and cmd.exe doesn't honor it: an element like 'a" & echo X & "' ran a second command. The pattern now accepts a colon there too. Harness section AE runs the injection payload through C:cmd.exe, C:cmd and C:CMD.EXE. from System32.
…ecks

Request-PkAbort and Ctrl+C only raise a flag; the engines stop on their token, which Test-PkAbortSignal cancels, and each main loop called it only after its launch step. So an abort requested before a runspace batch, or by a -ResultHandler, -NextJobProvider or the dispatcher's -PollCallback, still started a round of workers or programs first, and one raised by -JobFinalize had the dispatcher ask -GroupProvider for more groups.

Both main loops now check the flag first, the dispatcher checks it again after -PollCallback, and the runspace engine's launch loop reads it per job. Harness section O covers each of those five cases.
…e it starts

With the dispatcher now checking for an abort before it asks for any group, a batch started while an abort is in force never handed its commands to the dispatcher, so they came back with no results. The batch now reports each one as aborted itself. The harness's aborted-group callback test raises its abort once the group is built, since one already in force means no group is asked for, and section O checks that case for the dispatcher.
…t exited

When a program's output reader couldn't start, the rollback stopped the program with Stop-PkProcessTree, which passes over a program that has already exited, so a child it left running was never stopped. The dispatcher's cleanup when a -JobStarter throws after starting a program had the same gap. Both now kill the program's tree job first, which reaches its children whether or not it is still running, and fall back to Stop-PkProcessTree only when it has none. The rollback now also releases the tree job's handle and disposes the process, as the dispatcher's cleanup already disposed it.

A test hook, PkStreamReadTask.TestFailRunDelayMs, holds the failing reader start so the program has exited by then; TypesVersion is 16. Harness section AW covers the rollback on both start paths and the dispatcher's cleanup.
Once a program exits, -KillTreeOnExit ends what it left running through the program's own Job Object, and discarded the result: if Windows wouldn't end that Job Object, the leftovers kept running with no sign, showing only as a stream timeout if they held the output. There is no fallback once the program is gone (taskkill /T finds children only through their parent), so it now warns, once per session, naming the process, like the warning for a program without a Job Object of its own.

A test hook, PkChildProcessJob.TestFailTerminate, makes TerminateJobObject count as refused. Harness section BJ covers the batch, the dispatcher and Invoke-PkProcessWithTimeout, and that the warning shows once. The README's -KillTreeOnExit paragraph mentions it.
A function's scriptblock (${function:Name}) has the function as its Ast, which has no ParamBlock, so Invoke-PkRunspaceBatch saw no parameters: a job with values was refused ("doesn't declare it"), a declared $CancelToken wasn't filled in, and mandatory parameters weren't checked up front. The worker-safety check also counted the function's own body as a nested function, so a $script: at its top level was flagged, though the same body as a { } block isn't.

The parameters are now read from function f($a) or from the body's param() block, as Get-PkHandlerParamCount already does for callbacks, and the safety check treats the function's own body as the worker's top level. Harness section BC covers both function forms.
Windows reads a command line up to its first NUL, so an argument holding one was cut off there, and every argument after it was dropped, silently: cmd /c echo before<NUL>after next printed only "before". It's now an error before the program starts, naming the element for a list (also from ConvertTo-PkArgumentString), and for a string -Arguments, on both start paths and in every engine; in a batch it is that command's StartError. The README's Arguments section says so. Harness section AE covers a list, a string, Start-PkProcess, the batch and ConvertTo-PkArgumentString.
Once a program exits, -KillTreeOnExit ends what it left running through the program's own Job Object, and discarded the result: if Windows wouldn't end that Job Object, the leftovers kept running with no sign, showing only as a stream timeout if they held the output. There is no fallback once the program is gone (taskkill /T finds children only through their parent), so it now warns, once per session, naming the process, like the warning for a program without a Job Object of its own.

A test hook, PkChildProcessJob.TestFailTerminate, makes TerminateJobObject count as refused. Harness section BJ covers the batch, the dispatcher and Invoke-PkProcessWithTimeout, and that the warning shows once. The README's -KillTreeOnExit paragraph mentions it.
Base automatically changed from fix/function-worker-script to main December 3, 2026 01:06
@HalRiveria
HalRiveria merged commit 82517ef into main Dec 3, 2026
6 checks passed
@HalRiveria
HalRiveria deleted the fix/refuse-nul-argument branch December 3, 2026 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants